/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worlds;

import net.worlds.input.ConsoleReader;

public class WorldsCLI implements Runnable {
    public static boolean exit = false;
    private static Thread animator;
    private static Thread console;
    private static long serverID;
    private static String result;
    private static final long startTime = System.currentTimeMillis();
    private static Thread status;

    public static void main(String[] args) {
        try {
            Main.noView = true;
            status = new Thread(new Runnable() {
                public void run() {
                    while (!exit) {
                        try {
                            if (Main.getServer() != null) {
                                try {
                                    System.err.println("STATUS: " + Main.getServer().getPlayers().size() + " " + Main.getGame().getMainWorld().getService("NetworkServer").getProp("MaximumPlayers"));
                                } catch (WorldsException ex) {
                                    ex.printStackTrace();
                                }
                                Util.performRequestWithCallback(new Runnable() {

                                    public void run() {
                                        try {
                                            result = Util.httpGet("http://127.0.0.1:22000/?x=sr&s=" + serverID).trim();
                                        } catch (Exception ex) {
                                            result = "0";
                                            ex.printStackTrace();
                                        }
                                    }
                                }, new Runnable() {

                                    public void run() {
                                        if (result.equals("0") || result.isEmpty()) {
                                            exit = true;
                                        }
                                    }
                                });
                            }
                            try {
                                Thread.sleep(5000);
                            } catch (InterruptedException ex) {
                            }
                        } catch (Exception ex) {
                        }
                    }
                }
            });
            status.setName("StatusReporter");
            status.start();
            Main.joinScript = args[0];
            if (args.length > 0) {
                serverID = Long.parseLong(args[1]);
            }
            Main.init(null);
            animator = new Thread(new WorldsCLI());
            animator.setName("2DWorldsMain");
            animator.start();
            Main.mainThread = animator;
            console = new Thread(new ConsoleReader());
            console.setName("ConsoleThread");
            console.start();
                    } catch (WorldsException ex) {
            exit = true;
            ex.printStackTrace();
        }
    }

    public void run() {
        try {
            while (Thread.currentThread() == animator && !exit) {
                Main.gameStep();
            }
            System.err.println("ERROR: exited");
            System.exit(0);
            Main.exit();
        } catch (Exception ex) {
            Main.error = true;
            System.err.println("ERROR: " + ex);
            Main.error(ex);
        }
    }
}
